//
//  ResetLoginPasswordViewController.m
//  Finance
//
//  Created by Hu Jianguo on 13-8-10.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "ResetLoginPasswordViewController.h"
#import "AnalysisJson.h"
#import "MultiThreading.h"
#import "GetRegisterCodeViewController.h"
@interface ResetLoginPasswordViewController ()

@end

@implementation ResetLoginPasswordViewController
@synthesize username,password,confirmpassword,registercode;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated
{
    AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(mydelegate.registercode != nil)
    {
        self.registercode.text = mydelegate.registercode;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.username.delegate = self;
    self.password.delegate = self;
    self.confirmpassword.delegate = self;
    self.registercode.delegate = self;
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [leftItem setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [self.navigationItem setLeftBarButtonItem:leftItem];
 
    
    UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"beij.png"]];
    [self.view setBackgroundColor:bk];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -
-(IBAction)resetPassword:(id)sender
{
    [self.username resignFirstResponder];
    
    if(self.username.text == nil)
    {
        [self showMessageBox:@"提示" content:@"用户名不能空."];
        return;
    }
    
    if(self.username.text == nil || self.confirmpassword.text == nil)
    {
        [self showMessageBox:@"提示" content:@"用户的密码不能空."];
        return;
    }
    
    if(self.registercode.text == nil)
    {
        [self showMessageBox:@"提示" content:@"验证码不能为空."];
        return;
    }
    
    [self sendRequest];
}

-(IBAction)getRegisterCode:(id)sender
{
    GetRegisterCodeViewController* registercontroller = [[GetRegisterCodeViewController alloc] initWithNibName:@"GetRegisterCodeViewController" bundle:nil];
    [self.navigationController pushViewController:registercontroller animated:YES];
}

-(void)sendRequest
{
    //组请求报文
    NSDictionary *header = [AnalysisJson PackageJsonHeader:@"W118"];
    
    NSMutableDictionary *body = [[NSMutableDictionary alloc] init];
    
    body[@"username"]= self.username.text;
    body[@"password"] = self.password.text;
    body[@"valiCode"]=self.registercode.text;
    
    AppDelegate* mydelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(mydelegate != nil)
    {
         body[@"valiCodeSeq"] = mydelegate.registerseq;
    }
   

    NSData *requestJson = [AnalysisJson PackageJsonFromHeader:header andBody:body];
//    NSString *jsonString = [[NSString alloc] initWithData:requestJson encoding:NSUTF8StringEncoding];
    
    //发送报文
    [self getUserResetInfo: requestJson];
}

-(void)getUserResetInfo:(NSData*)jsonData
{
    [MultiThreading getDataFromNetwork:[SERVICE_URL stringByAppendingString:@"ResetPassword"] toObject:self callBack:@selector(getResetUerCallBack:) withBody:jsonData];
}

-(void)getResetUerCallBack:(NSDictionary*)dict
{
    NSDictionary* header = dict[@"header"];
    //NSDictionary* body = dict[@"body"];
    
    if(header != nil)
    {        
        [self showMessageBox:@"登录提示" content:header[@"returnMessage"]];
    }
 
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma  - textfield

-(void)textFieldDidEndEditing:(UITextField *)textField
{

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([self.username isFirstResponder])
    {
        [self.password becomeFirstResponder];
    }
    else if([self.password isFirstResponder])
    {
        [self.confirmpassword becomeFirstResponder];
    }
    else if([self.confirmpassword isFirstResponder])
    {
        [self.registercode becomeFirstResponder];
    }
    else if([self.registercode isFirstResponder])
    {
        [self.registercode resignFirstResponder];
    }
    return  YES;
}


-(void)showMessageBox:(NSString*)title content:(NSString*)content
{
    UIAlertView* msg = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [msg show];
}

@end
